package com.gh.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.gh.result.Result;
import com.gh.service.MsmService;
import com.gh.util.CodeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author Eric
 * @Date 2021/6/17 23:43
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/msm")
public class MsmApiController {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate<String, String> template;

    @GetMapping("send/{phone}")
    public Result sendMsmCode(@PathVariable String phone) {
        String code = template.opsForValue().get(phone);
        if(!StringUtils.isEmpty(code)){
            return Result.ok();
        }
        code = CodeUtil.getCode();
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        boolean isSend = msmService.sendCode(map, phone);
        if (isSend) {
            template.boundValueOps(phone).set(code, 5, TimeUnit.MINUTES);
        }
        return Result.ok();
    }

}
